//package com.ruoyi.procured.service.impl;
//
//import java.math.BigDecimal;
//import java.util.ArrayList;
//import java.util.List;
//
//import com.ruoyi.common.constant.Constants;
//import com.ruoyi.common.core.domain.OrdersProduct;
//import com.ruoyi.common.utils.DateUtils;
//import com.ruoyi.finance.domain.Advance;
//import com.ruoyi.finance.domain.Payable;
//import com.ruoyi.finance.mapper.AdvanceMapper;
//import com.ruoyi.finance.mapper.PayableMapper;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import com.ruoyi.procured.mapper.RequestAdvanceMapper;
//import com.ruoyi.procured.domain.RequestAdvance;
//import com.ruoyi.procured.service.IRequestAdvanceService;
//
///**
// * 申请预付款Service业务层处理
// *
// * @author ruoyi
// * @date 2023-11-22
// */
//@Service
//public class RequestAdvanceServiceImpl implements IRequestAdvanceService
//{
//    @Autowired
//    private RequestAdvanceMapper requestAdvanceMapper;
//
//    @Autowired
//    private AdvanceMapper advanceMapper;
//
//    @Autowired
//    private PayableMapper payableMapper;
//
//    /**
//     * 查询申请预付款
//     *
//     * @param requestId 申请预付款主键
//     * @return 申请预付款
//     */
//    @Override
//    public RequestAdvance selectRequestAdvanceByRequestId(Long requestId)
//    {
//        return requestAdvanceMapper.selectRequestAdvanceByRequestId(requestId);
//    }
//
//    /**
//     * 查询申请预付款列表
//     *
//     * @param requestAdvance 申请预付款
//     * @return 申请预付款
//     */
//    @Override
//    public List<RequestAdvance> selectRequestAdvanceList(RequestAdvance requestAdvance)
//    {
//        return requestAdvanceMapper.selectRequestAdvanceList(requestAdvance);
//    }
//
//    /**
//     * 新增申请预付款
//     *
//     * @param requestAdvance 申请预付款
//     * @return 结果
//     */
//    @Override
//    public int insertRequestAdvance(RequestAdvance requestAdvance)
//    {
//        requestAdvance.setPaymentType(Constants.FAIL);
//        requestAdvance.setCreateTime(DateUtils.getNowDate());
//        if(requestAdvance.getAddMoney() != null){
//            requestAdvance.setRequestAmount(requestAdvance.getRequestAmount().add(requestAdvance.getAddMoney()));
//        }
//        if(requestAdvance.getCutMoney() != null){
//            requestAdvance.setRequestAmount(requestAdvance.getRequestAmount().subtract(requestAdvance.getCutMoney()));
//        }
//        requestAdvance.setAmountRequested(requestAdvance.getRequestAmount());
//        int rows = requestAdvanceMapper.insertRequestAdvance(requestAdvance);
//        Advance advance = new Advance();
//        advance.setRequestId(requestAdvance.getRequestId());
//        advance.setCreateTime(DateUtils.getNowDate());
//        advanceMapper.insertAdvance(advance);
//        return rows;
//    }
//
//    @Override
//    public int insertRequestAdvanceQuash(RequestAdvance requestAdvance) {
//        requestAdvance.setPaymentType(Constants.SUCCESS);
//        requestAdvance.setCreateTime(DateUtils.getNowDate());
//        List<Payable> payableList = new ArrayList<>();
//        for (OrdersProduct ordersProduct:requestAdvance.getOrdersProductListQuash()) {
//            requestAdvance.setProductId(ordersProduct.getProductId());
//            requestAdvance.setRequestRemark(ordersProduct.getRequestRemark());
//            requestAdvance.setRequestAmount(ordersProduct.getAmountRequestedQuash());
//            requestAdvance.setAmountRequested(ordersProduct.getAmountRequestedQuash());
//            requestAdvanceMapper.insertRequestAdvance(requestAdvance);
//            Payable payable = new Payable();
//            payable.setRequestId(requestAdvance.getRequestId());
//            payable.setCreateTime(DateUtils.getNowDate());
//            payableList.add(payable);
//        }
//        return payableMapper.insert(payableList);
//    }
//
//    /**
//     * 修改申请预付款
//     *
//     * @param requestAdvance 申请预付款
//     * @return 结果
//     */
//    @Override
//    public int updateRequestAdvance(RequestAdvance requestAdvance)
//    {
//        requestAdvance.setUpdateTime(DateUtils.getNowDate());
//        requestAdvance.setApplicant(null);
//        requestAdvance.setApplicationDate(null);
//        //实付金额
//        BigDecimal requestAmount = requestAdvance.getRequestAmount();
//        //申请金额
//        BigDecimal amountRequested = requestAdvance.getAmountRequested();
//        int result = amountRequested.compareTo(requestAmount);
//        if(result>0){
//            requestAdvance.setRequestRecording("( -"+amountRequested.subtract(requestAmount)+")");
//        }else if(result < 0) {
//            requestAdvance.setRequestRecording("( +"+requestAmount.subtract(amountRequested)+")");
//        } else {
//            requestAdvance.setRequestRecording("");
//        }
//        return requestAdvanceMapper.updateRequestAdvance(requestAdvance);
//    }
//
//    /**
//     * 批量删除申请预付款
//     *
//     * @param requestIds 需要删除的申请预付款主键
//     * @return 结果
//     */
//    @Override
//    public int deleteRequestAdvanceByRequestIds(Long[] requestIds)
//    {
//        int rows = requestAdvanceMapper.deleteRequestAdvanceByRequestIds(requestIds);
//        advanceMapper.deleteAdvanceByAdvanceIds(requestIds);
//        return rows;
//    }
//
//    /**
//     * 删除申请预付款信息
//     *
//     * @param requestId 申请预付款主键
//     * @return 结果
//     */
//    @Override
//    public int deleteRequestAdvanceByRequestId(Long requestId)
//    {
//        return requestAdvanceMapper.deleteRequestAdvanceByRequestId(requestId);
//    }
//
//    @Override
//    public RequestAdvance selectTotalAmountDue(Long ordersId) {
//        return requestAdvanceMapper.selectTotalAmountDue(ordersId);
//    }
//
//    /**
//     * 审核完成需要取消申请按钮
//     * @param requestIds 申请预付款主键
//     * @return 结果
//     */
//    @Override
//    public int updateRequestAdvanceCancelStatus(Long[] requestIds) {
//        return requestAdvanceMapper.updateRequestAdvanceCancelStatus(requestIds);
//    }
//
//    /**
//     * 付款完成需要修改申请记录的付款状态
//     * @param requestIds 申请预付款主键
//     * @return 结果
//     */
//    @Override
//    public int updateRequestAdvancePaymentStatus(Long[] requestIds) {
//        return requestAdvanceMapper.updateRequestAdvancePaymentStatus(requestIds);
//    }
//}
